function YearBack(urlYear, urlMonth, urlList){
	$("h6 a:first-child").click(function(event){
		event.preventDefault();
		var archive = $("#Archive");
		var d = new Date();
		$.post(urlYear +"/" + d.getFullYear(),
		function(data){
			archive.children("ul").empty();
			$.each(data, function(item){
				if(data[item]>0){
					archive.children("ul").append("<li><a href=\"#\" class=\"Year\">" + item + " (" + data[item] + ")</a></li>");
				} else{
					archive.children("ul").append("<li><span>" + item + " (" + data[item] + ")</span></li>");
				}
			});
			archive.children("h6").children("span:last-child").remove();
			archive.children("h6").append('<a href=\"#\">&gt;&gt;</a>');
			archive.children("h6").children("a:first-child").remove();
			archive.children("h6").prepend('<span>&lt;&lt;</span>')
			InitializeArchive(urlYear, urlMonth, urlList);
			YearForward(urlYear, urlMonth, urlList);
		});
		return false;
	});
}

function YearForward(urlYear, urlMonth, urlList){
	$("h6 a:last-child").click(function(event){
		event.preventDefault();
		var archive = $("#Archive");
		$.post(urlYear + "/2004",
			function(data){
				archive.children("ul").empty();
				$.each(data, function(item){
					if(data[item]>0){
						archive.children("ul").append("<li><a href=\"#\" class=\"Year\">" + item + " (" + data[item] + ")</a></li>");
					} else{
						archive.children("ul").append("<li><span>" + item + " (" + data[item] + ")</span></li>");
					}
				});
				archive.children("h6").children("span:first-child").remove();
				archive.children("h6").prepend('<a href=\"#\">&lt;&lt;</a>');
				archive.children("h6").children("a:last-child").remove();
				archive.children("h6").append('<span>&gt;&gt;</span>');
				InitializeArchive(urlYear, urlMonth, urlList);
				YearBack(urlYear, urlMonth, urlList);
			});
		return false;
	});
}

function MonthBack(urlYear, urlMonth, urlList, year){
	var archive = $("#Archive");
	archive.children("h6").children("a:first-child").click(function(event){
		event.preventDefault();
		$.post(urlMonth + "/" + year.substring(0,4) + "/" + 1,
			function(data){
				archive.children("ul").empty();
				$.each(data, function(item){
					if(data[item]>0){
						archive.children("ul").append("<li><a href=\"" + urlList + "/" + year.substring(0,4) + "/" + item + "\">" + item + " (" + data[item] + ")</a></li>");
					}else{
						archive.children("ul").append("<li><span>" + item + " (" + data[item] + ")</span></li>");
					}
				});
				archive.children("h6").children("span:last-child").remove();
				archive.children("h6").append('<a href=\"#\">&gt;&gt;</a>');
				archive.children("h6").children("a:first-child").remove();
				archive.children("h6").prepend('<span>&lt;&lt;</span>')
				InitializeArchive(urlYear, urlMonth, urlList);
				MonthForward(urlYear, urlMonth, urlList, year);
			});
		return false;
	});
}

function MonthForward(urlYear, urlMonth, urlList, year){
	var archive = $("#Archive");
	archive.children("h6").children("a:last-child").click(function(event){
		event.preventDefault();
		$.post(urlMonth + "/" + year.substring(0,4) + "/" + 7,
			function(data){
				archive.children("ul").empty();
				$.each(data, function(item){
					if(data[item]>0){
						archive.children("ul").append("<li><a href=\"" + urlList + "/" + year.substring(0,4) + "/" + item + "\">" + item + " (" + data[item] + ")</a></li>");
					}else{
						archive.children("ul").append("<li><span>" + item + " (" + data[item] + ")</span></li>");
					}
				});
				archive.children("h6").children("span:first-child").remove();
				archive.children("h6").prepend('<a href=\"#\">&lt;&lt;</a>');
				archive.children("h6").children("a:last-child").remove();
				archive.children("h6").append('<span>&gt;&gt;</span>');
				InitializeArchive(urlYear, urlMonth, urlList);
				MonthBack(urlYear, urlMonth, urlList, year);
			});
		return false;
	});
}

function MonthToYear(urlYear, urlMonth, urlList){
	var archive = $("#Archive")
	archive.children("h6").children("a:nth-child(2)").click(function(event){
		event.preventDefault();
		var archive = $("#Archive");
		var d = new Date();
		$.post(urlYear +"/" + d.getFullYear(),
			function(data){
				archive.empty();
				archive.append($("<h4>Archive</h4>").hide());
				archive.append($("<ul></ul>").hide());
				archive.append($("<h6><span>&lt;&lt;</span> <span>year</span> <a href=\"#\">&gt;&gt;</a></h6>").hide());
				$.each(data, function(item){
					if(data[item]>0){
						archive.children("ul").append("<li><a href=\"#\" class=\"Year\">" + item + " (" + data[item] + ")</a></li>");
					} else{
						archive.children("ul").append("<li><span>" + item + " (" + data[item] + ")</span></li>");
					}
				});
				archive.children("ul").fadeIn("slow");
				archive.children("h4").fadeIn("slow");
				archive.children("h6").fadeIn("slow");
				InitializeArchive(urlYear, urlMonth, urlList);
				YearForward(urlYear, urlMonth, urlList);
			});
		return false;
	});
}		

function InitializeArchive(urlYear, urlMonth, urlList){
	$(".Year").click(function(event) {
		event.preventDefault();
		var archive = $("#Archive");
		$.post(urlMonth + "/" + event.target.innerHTML.substring(0,4) + "/" + 1,
			function(data){	   					
				archive.empty();
				archive.append($("<h4>Archive " + event.target.innerHTML.substring(0,4) + "</h4>").hide());
				archive.append($("<ul></ul>").hide());
				archive.append($("<h6><span>&lt;&lt;</span> <a href=\"#\">year</a> <a href=\"#\">&gt;&gt;</a></h6>").hide());
				$.each(data, function(item){
					if(data[item]>0){
						archive.children("ul").append("<li><a href=\"" + urlList + "/" + event.target.innerHTML.substring(0,4) + "/" + item + "\">" + item + " (" + data[item] + ")</a></li>");
					}else{
						archive.children("ul").append("<li><span>" + item + " (" + data[item] + ")</span></li>");
					}
				});
				archive.children("ul").fadeIn("slow");
				archive.children("h4").fadeIn("slow");
				archive.children("h6").fadeIn("slow");
				
				MonthForward(urlYear, urlMonth, urlList, event.target.innerHTML);
				MonthToYear(urlYear, urlMonth, urlList);
		});
		return false;
	});
}